<?php

namespace App\Http\Controllers\Admin;
use App\Models\BucketCategory;
use App\Models\Shop;
use App\Models\ShopBucketCategory;


/**
 * 商品分类管理控制器
 */
class BucketCategoryController extends BaseController
{

    public function __construct(BucketCategory $bucketCategory,Shop $shop,ShopBucketCategory $shopBucketCategory)
    {
        parent::__construct();
        $this->bucketCategory = $bucketCategory;
        $this->shopBucketCategory = $shopBucketCategory;
        $this->shop = $shop;
    }

    /**
     * 分类管理列表
     */
    public function getIndex()
    {
        $shopList = $this->shop->_getList(['admin_id'=>is_login()])->toArray();
        $tree = $this->bucketCategory->_getList(['admin_id'=>is_login()],'*',['sort'=>'asc'])->toArray();
        return view('admin.bucket_category.index', compact('tree','shopList'));
    }

    public function getShopIndex()
    {
        $shop = $this->shop->_getOneById(request('shop_id'));
        $cate = $this->shopBucketCategory->_getOne(['shop_id'=>request('shop_id')]);
        if($cate) {
            $cate = $cate->toArray();
        }
        return view('admin.bucket_category.shop_index', compact('cate','shop'));
    }

    public function getStore()
    {
        if (!request('shop_id')) {
            return $this->error('非法操作');
        }
        $info = $this->shopBucketCategory->_getOne(['shop_id'=>request('shop_id')]);
        if($info) {
            $info = $info->toArray();
            $info['ids'] = [];
            foreach($info['category_data'] as $k=>$v) {
                $info['ids'][] = $v['id'];
            }
        }
        $rows = $this->bucketCategory->_getList(['admin_id'=>is_login(),'status'=>1],'*',['sort'=>'asc'])->toArray();
        return view('admin.bucket_category.store', compact('rows','info'));
    }

    public function postStore()
    {
        $res = $this->shopBucketCategory->addOrUpdate();
        if (!$res) {
            return $this->error($this->shopBucketCategory->getError() ? $this->shopBucketCategory->getError() : '未知错误');
        }
        return $this->success('操作成功', urlAdmin('bucketCategory', 'getShopIndex', ['shop_id' => request('shop_id')]));
    }


    public function getAdd()
    {
        $category = $this->bucketCategory->checkParentCategory();
        if (!$category) {
            return $this->error($this->bucketCategory->getError() ? $this->bucketCategory->getError() : '未知错误');
        }
        $info['pid'] = request('pid');
        return view('admin.bucket_category.edit', compact('category', 'info'));
    }

    public function postAdd()
    {
        $res = $this->bucketCategory->addOrUpdate();
        if (!$res) {
            return $this->error($this->bucketCategory->getError() ? $this->bucketCategory->getError() : '新增失败');
        }
        if(request('shop_id')) {
            return $this->success('新增成功', urlAdmin('bucketCategory', 'getShopIndex', ['shop_id' => request('shop_id')]));
        }
        return $this->success('新增成功', urlAdmin('bucketCategory', 'getIndex'));
    }

    /* 编辑分类 */
    public function getEdit()
    {
        $category = $this->bucketCategory->checkParentCategory();
        if (!$category) {
            return $this->error($this->bucketCategory->getError() ? $this->bucketCategory->getError() : '未知错误');
        }
        /* 获取分类信息 */
        $info = request('id') ? $this->bucketCategory->getInfo(request('id')) : '';
        return view('admin.bucket_category.edit', compact('info', 'category'));
    }

    public function postEdit()
    {
        $res = $this->bucketCategory->addOrUpdate();
        if (!$res) {
            return $this->error($this->bucketCategory->getError() ? $this->bucketCategory->getError() : '未知错误');
        }
        return $this->success('更新成功', urlAdmin('bucketCategory', 'getIndex', ['pid' => request('pid')]));
    }

    /* 新增分类 */
    public function getAddShopCategory()
    {
        $category = $this->bucketCategory->checkParentCategory();
        if (!$category) {
            return $this->error($this->bucketCategory->getError() ? $this->bucketCategory->getError() : '未知错误');
        }
        $info['pid'] = request('pid');
        return view('admin.bucket_category.edit', compact('category', 'info'));
    }

    public function postAddShopCategory()
    {
        $res = $this->bucketCategory->addOrUpdate();
        if (!$res) {
            return $this->error($this->bucketCategory->getError() ? $this->bucketCategory->getError() : '新增失败');
        }
        return $this->success('新增成功', urlAdmin('bucketCategory', 'getIndex', ['pid' => request('pid')]));
    }



    /**
     * 删除一个分类
     */
    public function anyDelShopCate()
    {
        $res = $this->shopBucketCategory->del();
        if (!$res) {
            return $this->error($this->shopBucketCategory->getError() ? $this->shopBucketCategory->getError() : '新增失败');
        }
        return $this->success('删除成功', urlAdmin('bucketCategory', 'getShopIndex', ['shop_id' => request('shop_id')]));
    }

    /**
     * 操作分类初始化
     */
    public function getOperate($type)
    {
        //检查操作参数
        $operate = $type == 'move' ? '移动' : ($type == 'merge' ? '合并' : '');
        if (!$operate) {
            return $this->error('参数错误！');
        }
        $from = intval(request('from'));
        if (empty($from)) {
            return $this->error('参数错误！');
        }
        //获取分类
        $where = ['status' => 1, 'id' => ['<>', $from]];
        $list = $this->bucketCategory->_getList($where, 'id,pid,title')->toArray();
        //移动分类时增加移至根分类
        if (strcmp($type, 'move') == 0) {
            //不允许移动至其子孙分类
            $list = tree_to_list(list_to_tree($list));
            $category = $this->bucketCategory->_getOneById($from, 'pid');
            $category['pid'] && array_unshift($list, ['id' => 0, 'title' => '根分类']);
        }
        return view('admin.bucket_category.operate', compact('type', 'operate', 'from', 'list'));
    }

    public function postOperate()
    {
        $res = $this->bucketCategory->operate(request('type'));
        if (!$res) {
            return $this->error($this->bucketCategory->getError());
        }
        return $this->success($res, urlAdmin('bucketCategory', 'getIndex'));
    }

}
